package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.enception.BusinessException;
import com.qk.entity.User;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> list(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());

        List<User> userList = userMapper.selectList(userDto);

        Page<User> page = (Page<User>) userList;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void add(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        String password = user.getPassword() + "123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        userMapper.insert(user);
    }

    @Override
    public void deleteById(List<Integer> ids) {
        userMapper.deleteByIds(ids);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        user.setPassword(DigestUtils.md5DigestAsHex((user.getUsername() + "123").getBytes()));
        userMapper.update(user);
    }

    @Override
    public List<User> listAll() {
        return userMapper.listAll();
    }

    @Override
    public List<User> userByRole(String roleLabel) {
        return userMapper.userByRole(roleLabel);
    }

    @Override
    public List<User> userByDept(Number deptId) {
        return userMapper.userByDept(deptId);
    }

    @Override
    public LoginResultVo login(User user) {
        //调用userMapper
        User u  = userMapper.selectByUsername(user.getUsername());
        //判断用户是否存在
        if (u == null){
            throw new BusinessException("用户名或密码不正确");
        }
        //判断密码是否正确
        String pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if (!pass.equals(u.getPassword())){
            log.info("密码错误");
            throw new BusinessException("用户名或密码不正确");
        }
        //判断状态是否正确
        if (u.getStatus() != 1){
            throw new BusinessException("用户已停用");
        }
        //封装结果并返回
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleName());

        //调用工具类,生成jwt令牌
        Map<String, Object> claimes = new HashMap<>();
        claimes.put("id", u.getId());
        claimes.put("username", u.getUsername());
        String jwt = JwtUtils.generateToken(claimes);

        loginResultVo.setToken(jwt);
        return loginResultVo;
    }
}
